/**
 * 贪心算法，cur记录数组中每相邻的两数之差， sum += cur
 * 相加后如果sum < 0，就把 sum 重置为 0
 * @param {number[]} prices
 * @return {number}
 */
const maxProfit = prices => {

  let cur = sum = max = 0

  for (let i = 0; i < prices.length - 1; i++) {
    cur = prices[i + 1] - prices[i]
    sum += cur
    if (sum < 0) sum = 0
    max = Math.max(sum, max)
  }
  return max
}


console.log(maxProfit([7, 1, 5, 3, 6, 4]))